<?php
/**
 * 提供给其他地三方使用API接口
 * @author coldarmy
 * **/
apf_require_class('Bll_Attendance_UserInfo');
apf_require_class('Bll_Attendance_OrgRelation');
apf_require_class('APF_Controller');

apf_require_class('Bll_Hr_OutInterface');

class Oa_Attendance_UserAPIController extends APF_Controller {

    private $bll_user;
    private $bll_org;

    public function handle_request() {
        $request = APF::get_instance()->get_request();
        $params = $request->get_parameters();
        $this->bll_user = new Bll_Attendance_UserInfo();
        $this->bll_org = new Bll_Attendance_OrgRelation();
        $params['version'] = empty($params['version']) ? 0 : 1;

        $params['version']=1;
        if($params['version']){
            $echo1 = $this->_get_user_by_hroa($params);
            $echo=array();//数组索引居然必须是从0开始
            if(!empty($echo1)){
                foreach($echo1 as $val){
                    $echo[]=$val;
                }
            }
        }else{
            $echo = $this->get_user_info($params);
        }
        echo json_encode($echo);
        exit;
    }
    /**
     * HROA系统数据
     * @param unknown_type $params
     * @return multitype:|Ambigous <multitype:, unknown>
     */
    private function _get_user_by_hroa($params){
        $result = array();
        if(empty($params) || empty($params['keyword'])) {
            return $result;
        }
        $auth = $params['auth'];
        $keyword = $params['keyword'];
        $superior = isset($params['superior'])? $params['superior']:0;
        $subordinate = isset($params['subordinate'])? $params['subordinate']:0;
        /*$sys_rsa = 'ajkzwww';
        $check_auth = md5(md5($keyword).$sys_rsa);
        //验证auth
        if($auth != $check_auth) {
            return array('验证错误！');
        }*/

        $is_match_key = $this->_match_key($auth, $keyword);
        if(!$is_match_key){
            return array('验证错误！');
        }
        $bll = new Bll_Hr_OutInterface();
        $columns = explode(',',$params['columns']);
        $data = $bll->search_user($keyword,$columns,$superior,$subordinate);
        $res = $this->_build_data_by_app($data,$is_match_key,$superior,$subordinate);
        return $res ;
    }
    /**
     * 根据应用返回对应字段
     * @param array $data
     * @param string $rsa_key
     */
    private function _build_data_by_app($data,$rsa_key,$superior,$subordinate){
        $app_field = $this->_api_key_field($rsa_key);
        $res = array();
        foreach ($data as $k=>$val){
            foreach ($app_field as $field){
                if($field=='superior' && !$superior){//不需要返回上级
                    continue;
                }
                if($field=='subordinate' && !$subordinate){//不需要返回下级
                    continue;
                }
                $res[$k][$field] = $val[$field];
            }
        }
        return $res;
    }
    private function get_user_info($params) {
        $result = array();
        if(empty($params) || empty($params['keyword'])) {
            return $result;
        }
        $auth = $params['auth'];
        $keyword = $params['keyword'];
        $superior = isset($params['superior'])? $params['superior']:0;
        $subordinate = isset($params['subordinate'])? $params['subordinate']:0;
        $sys_rsa = 'ajkzwww';
        $check_auth = md5(md5($keyword).$sys_rsa);
        //验证auth
        if($auth != $check_auth) {
            return array('验证错误！');
        }

        $columns = explode(',',$params['columns']);
        if(in_array('user_id',$columns)){
            $where['user_id'] = $keyword;
        }else{
            $where[$this->build_where($columns,$keyword)] = 1;
        }
        $tmp = $this->bll_user->get_user_by_condition($where);
        if(empty($tmp)) {
            return $result;
        }
        //封装数据
        foreach ($tmp as $key => $val) {
            $result[$key]['user_id'] = $val['user_id'];
            $result[$key]['name'] = $val['name'];
            $result[$key]['code'] = $val['code'];
            $result[$key]['email'] = $val['email'];
            $result[$key]['mobile'] = $val['mobile'];
            $result[$key]['enter_date'] = $val['enter_date'];
            $result[$key]['indue_date'] = $val['indue_date'];
            $result[$key]['dimission_date'] = $val['dimission_date'] == '0000-00-00 00:00:00'?0:$val['dimission_date'];
            $result[$key]['english_name'] = $val['english_name'];
            $result[$key]['department_name'] = $val['department_name'];
            $result[$key]['job_name'] = $val['position_name'];
            $result[$key]['city_name'] = $val['city_name'];
            if($superior) {
                $result[$key]['superior'] = $this->get_superior($val['user_id']);
            }
            if($subordinate) {
                $result[$key]['subordinate'] = $this->get_subordinate($val['user_id']);
            }
            $result[$key]['city_name'] = $val['city_name'];
        }
        return $result;
    }

    /**
     * 获取下属信息
     * @param int $user_id
     * @return array
     * **/
    private function get_subordinate($user_id) {
        $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $tmp = $this->bll_org->get_subordinate_by_user_id($user_id);
        if(empty($tmp)) {
            return $result;
        }
        $ids =  array();
        foreach ($tmp as $key => $val) {
            $ids[] = $val['user_id'];
        }
        $back = $this->bll_user->get_user_by_ids($ids);
        if(empty($back)) {
            return $result;
        }
        foreach ($back as $k => $v) {
            $result[$k]['user_id'] = $v['user_id'];
            $result[$k]['name'] = $v['name'];
            $result[$k]['code'] = $v['code'];
        }
        return $result;
    }

    /**
     * 获取上级信息
     * **/
    private function get_superior($user_id) {
         $result = array();
        if(empty($user_id)) {
            return $result;
        }
        $tmp = $this->bll_org->get_sup_id($user_id,1);
        $tmp = $this->bll_user->get_user_by_ids($tmp);

        if (empty($tmp)) {
            return $result;
        }
        $result['name'] = $tmp[0]['name'];
        $result['code'] = $tmp[0]['code'];
        $result['user_id'] = $tmp[0]['user_id'];
        return $result;
    }

    /**
     * 构建查询条件
     * @param array $data
     * @param string $keyword
     * @return string
     * **/
    private function build_where($data,$keyword) {
        $result = ' 1 != ? ';
        if(empty($data) || empty($keyword)) {
            return $result;
        }
        foreach ($data as $key => $val) {
            if(in_array($val,array('code','name','email'))) {
                $result .= ' OR '.$this->build_columns_condition($val,$keyword);
            }
            if($val == 'user_id') {
                $result = ' OR'.$this->build_columns_condition($val,$keyword);
                break;
            }
        }
        return $result;
    }

    /**
     * 构建字段查询的组合条件
     * @param string $columns
     * @param string $keyword
     * @return string
     * **/
    private function build_columns_condition($columns,$keyword) {
        $result = '';
        if($columns == 'code') {
            $result = " code = '{$keyword}' ";
        } elseif ($columns == 'name') {
            $result = " name like '%".$keyword."%' ";
        } elseif ($columns == 'email') {
            $result = " email like '%".$keyword."%' ";
        } elseif ($columns == 'user_id') {
            //$result = " user_id = '{$keyword}' ";
        }
        return $result;
    }
    private function _match_key($auth,$keyword){
        $key_list = $this->_api_key_list();
        $is_match = false;
        foreach($key_list as $sys_rsa){
            $check_auth = md5(md5($keyword).$sys_rsa);
            if($auth == $check_auth) {
                $is_match = $sys_rsa;
                break;
            }
        }
        return $is_match;
    }
    private function _api_key_list(){
        return array(
                'ajkzwww',//CRM在使用
                'ajkcfin',//财务部报销系统
        );
    }
    /**
     * 每个应用对应的能返回的字段
     * @return multitype:multitype:string
     */
    private function _api_key_field($rsa_key){
        $field_list = array(
                'ajkzwww'=>array(
                        'user_id','name','code','email','mobile',
                        'enter_date','indue_date','dimission_date',
                        'english_name','department_name','job_name','city_name','job_status',
                        'superior','subordinate'
                ),
                'ajkcfin'=>array(
                        'user_id','name','real_name','english_name','code',
                        'email','mobile','office_phone','join_keep','job_status',
                        'enter_date','indue_date','dimission_date','work_city_id',
                        'city_name','bu_name','department_name','job_name',
                        'bank_name','bank_card','superior','subordinate'
                ),
        );
        return $field_list[$rsa_key];
    }

}
